package top.juntower.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import top.juntower.dao.EnjoyFootMapper;
import top.juntower.pojo.vo.EnjoyFootVo;
import top.juntower.service.EnjoyFootService;
import top.juntower.utils.RedisUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EnjoyFootServiceImpl implements EnjoyFootService {

    @Autowired
    private EnjoyFootMapper footMapper;

    @Autowired
    private RedisUtils redisUti;

    @Override
    public String queryEnjoyFootById(Integer userId, Integer fId) {
        return footMapper.queryEnjoyFootById(userId, fId);
    }

    @Override
    public int addEnjoyFootState(Map map) {
        return footMapper.addEnjoyFootState(map);
    }

    @Override
    public List<EnjoyFootVo> queryFavoriteFoot(Integer userId) {
        return footMapper.queryFavoriteFoot(userId);
    }

    @Scheduled(cron = "0/3 * * * * ?")
    public  void  helloScheduler(){
        Map<Object, Object> isState = redisUti.hmget("isState");
        for (Map.Entry<Object, Object> entry : isState.entrySet()) {

            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            String stirng = (String) entry.getKey();
            String[] split = stirng.split("::");
            String userId = split[0];
            String fId = split[1];
            String s = footMapper.queryEnjoyFootUserId(Integer.parseInt(userId), Integer.parseInt(fId));
            System.out.println(s);
            Map<String, Object> map = new HashMap<>();
            map.put("userId",userId);
            map.put("fId",fId);
            map.put("efState",entry.getValue());
            map.put("efTime",new Date());
            if (s == null){
                footMapper.addEnjoyFootState(map);
                System.out.println("添加成功");
            }else {
                footMapper.updateEnjoyFootState(map);
                System.out.println("修改成功");
            }
        }
    }
}
